package com.pig.controller.cart;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pig.exception.ServiceException;
import com.pig.pojo.cart.Cart;
import com.pig.pojo.goods.Goods;
import com.pig.pojo.user.User;
import com.pig.service.cart.DubboCartService;
import com.pig.service.goods.GoodsService;
import com.pig.util.SysResult;
import com.pig.util.UserThreadLocal;
import org.apache.shiro.SecurityUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
public class CartController {

    @Reference(check = false)
    private GoodsService goodsService;
    @Reference(check = false)
    private DubboCartService cartService;

    /**
     * 添加购物车
     */
    @RequestMapping("/page/addCartByUserName")
    public SysResult addCartByUserId(Cart cart, HttpServletRequest request) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return SysResult.fail(new ServiceException("请登陆后再进行添加！"));
        }
        System.out.println("Shiro User >>> "+user);
        Integer userId = Math.toIntExact(user.getId());
        if (userId == null) {
            return SysResult.fail();
        }
        Integer cartId = new Random().nextInt(200000000);
        cart.setUserId(userId);
        cart.setCartId(cartId);
        cart.setCreatedTime(new Date());
        cartService.saveCart(cart);


        /*存入到数据库cart*/
        return SysResult.success();
    }
    /*test*/

    @RequestMapping("/page/cartShow")
    public SysResult cartShow() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer userId = Math.toIntExact(user.getId());
        List<Cart> cartList = cartService.findUserItems(userId);
        return SysResult.success(cartList);
    }

    /**
     * 购物车点击加减号修改商品数量
     * :8891/cat/changNum"
     */
    @RequestMapping("/cart/changNum")
    public SysResult changNum(Integer num, String cartId, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        Integer cartId1 = Integer.parseInt(cartId);
        cartService.changeNum(cartId1, num);
        return SysResult.success();
    }

    /**
     * 购物车点击删除单个商品
     * /cart/delItemCartById
     */
    @RequestMapping("/page/cartDelItemCartById")
    public SysResult delItemCartById(String cartId, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        System.out.println(cartId);
        Integer cartId1 = Integer.parseInt(cartId);
        cartService.delItemCartById(cartId1);
        return SysResult.success();
    }


}
